package com.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.configure.Result;
import com.food.entity.Food;
import com.food.entity.Order;
import com.food.entity.OrderFood;
import com.food.entity.SaleLog;
import com.food.mapper.FoodMapper;
import com.food.mapper.OrderFoodMapper;
import com.food.mapper.OrderMapper;
import com.food.mapper.SaleLogMapper;
import com.food.service.OrderService;
import com.food.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-04-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    OrderMapper orderMapper;

    @Resource
    OrderFoodMapper orderFoodMapper;

    @Resource
    FoodMapper foodMapper;

    @Resource
    SaleLogMapper saleLogMapper;

    @Override
    public Result<Order> updateOrder(Order order) {
        Result<Order> result = new Result<>();
        try {
            if (StringUtils.isEmpty(order.getOrderId())) {
                //如果ID为空，代表添加
                order.setOrderId(IDGenerator.getUUID("ORDER"));
                order.setOrderTime(new Date());
                order.setOrderStatus("0");
                order.setPayStatus("0");
                orderMapper.insert(order);

                if (order.getFoodList() != null) {
                    order.getFoodList().stream().forEach(orderFood -> {
                        orderFood.setOrderFoodId(IDGenerator.getUUID("ODDERFOOD"));
                        orderFood.setOrderId(order.getOrderId());
                        orderFoodMapper.insert(orderFood);

                        Food food = new Food();
                        food.setFoodId(orderFood.getFoodId());
                        food.setAmount(orderFood.getOrderAmount());
                        foodMapper.reduceFood(food);

                        SaleLog saleLog = new SaleLog();
                        saleLog.setSaleLogId(IDGenerator.getUUID("SALELOG"));
                        saleLog.setUserId(order.getCreateUser());
                        saleLog.setUserName(order.getCreateUserName());
                        saleLog.setUserType("1");
                        saleLog.setFoodId(orderFood.getFoodId());
                        saleLog.setFoodName(orderFood.getFoodName());
                        saleLog.setPrice(foodMapper.selectById(orderFood.getFoodId()).getPrice());
                        saleLog.setCreateTime(new Date());
                        saleLog.setAmount(orderFood.getOrderAmount());
                        saleLog.setTotalPrice(orderFood.getOrderAmount() * saleLog.getPrice());
                        saleLog.setPercentMoney(Double.parseDouble(String.format("%.2f",foodMapper.selectById(orderFood.getFoodId()).getPercent1() * saleLog.getTotalPrice())));
                        saleLogMapper.insert(saleLog);
                    });
                }
            } else {
                //如果ID不为空，代表修改
                orderMapper.updateById(order);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Order> delOrder(Order order) {
        Result<Order> result = new Result<>();
        try {
            if (StringUtils.isEmpty(order.getOrderId())) {
                result.setMessage("orderId can't be empty");
                return result;
            }
            orderMapper.deleteById(order.getOrderId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Order> batchDelOrder(Order order) {
        Result<Order> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(order.getIds())) {
                String[] ids = order.getIds().split(",");
                for (String id : ids) {
                    orderMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Order> queryOrder(Order order) {
        Result<Order> result = new Result<>();
        try {
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            List<Order> orders = orderMapper.selectList(wrapper);
            result.setReturnList(orders);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Order> queryOrderByPage(Order order) {
        Result<Order> result = new Result<>();

        try {
            if (order.getCurrent() == null || order.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            IPage<Order> orders = orderMapper.queryOrderByPage(new Page<>(order.getCurrent(), order.getSize()), order);
            result.setReturnPage(orders);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Order> queryOrderById(Order order) {
        Result<Order> result = new Result<>();
        try {
            Order returnOrder = orderMapper.selectById(order.getOrderId());
            result.setReturnObj(returnOrder);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
